<template>
  <div>
    <UploadExel :on-success="onSuccess" />
  </div>
</template>

<script>
import { ImportEmployeesAPI } from '@/api/employees'
import { formatDate } from '@/utils'
export default {
  data() {
    return {
      excelData: []
    }
  },
  methods: {
    async onSuccess({ results }) {
      console.log(results)
      // 提前准备好映射关系
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      // 通过map方法 改造数据
      this.excelData = results.map(item => {
        const obj = {}
        Object.keys(item).forEach(key => {
          if (key === '入职日期' || key === '转正日期') {
            return (obj[userRelations[key]] = formatDate(item[key], '/'))
          }
          obj[userRelations[key]] = item[key]
        })
        return obj
      })
      await ImportEmployeesAPI(this.excelData)
      this.$message.success('导入员工成功!')
      this.$router.back()
    }
  }
}
</script>

<style>

</style>
